package com.gg.common.realm;


import com.gg.common.pojo.SysUsers;
import com.gg.hj.service.SysUsersService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import javax.annotation.Resource;

/**
 * @author koco
 * 2022/9/20
 * 15:37
 * 注册和登录都通过这个realm,密码加密方式为:md5
 */
public class MyRealm extends AuthorizingRealm {
    @Resource
    private SysUsersService usersService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//        info.addRole("user");
//        info.addRole("admin");
//        info.addStringPermission("user:query");
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String username = authenticationToken.getPrincipal().toString();
        SysUsers login = usersService.loginService(username);
        ByteSource bytes = ByteSource.Util.bytes(username);
        return new SimpleAuthenticationInfo(username, login.getPassword(), bytes, getName());
    }
}
